<template>

  <el-container>
    <el-header>
      <img src="./assets/logo.png"
           class="logo">
      <span class="title">合众伟奇</span>
      <div class="user">
        <el-dropdown trigger="click">
          <div class="el-dropdown-link">
            <el-avatar src=""
                       icon="el-icon-user-solid"></el-avatar>
            <span class="name">{{user.token?user.name:'未登录'}}</span>
            <i style="color:#fff"
               class="el-icon-caret-bottom el-icon--right"></i>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item class="clearfix">
              <span @click="handleChangeLogin"> {{user.token?'退出登录':'立即登录'}}</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <el-aside>
        <el-menu router>
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-s-home"></i>
              <span>主应用</span>
            </template>
            <el-menu-item index="/">首页</el-menu-item>
            <el-menu-item index="/login">登录</el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-s-help"></i>
              <span>vue 微应用</span>
            </template>
            <el-menu-item index="/project1Rule">商品列表</el-menu-item>
            <el-menu-item index="/project1Rule/add">新增商品</el-menu-item>
          </el-submenu>
          <!-- <el-submenu index="3">
            <template slot="title">
              <i class="el-icon-s-help"></i>
              <span>uniApp 微应用</span>
            </template>
            <el-menu-item index="/project2Rule">uniApp</el-menu-item>
          </el-submenu> -->
          <!-- <el-submenu index="4">
            <template slot="title">
              <i class="el-icon-s-help"></i>
              <span>Jquery 微应用</span>
            </template>
            <el-menu-item index="/project3Rule">Jquery</el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>
      <el-main>

        <div id="container">
        </div>

        <router-view />

      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data() {
    return {
    }
  },
  computed: {
    user() {
      let user = localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : this.$store.state.user
      return user
    }
  },
  mounted() {
    // onGlobalStateChange 第二个入参为 true，代表立即执行一次观察者函数
    this.$actions.onGlobalStateChange((state, prevState) => {
      // state: 变更后的状态; prevState: 变更前的状态
      console.log("主应用观察者：新值", state.user);
      console.log("主应用观察者：旧值 ", prevState.user);
      this.$store.commit("setUser", state.user)
    });
  },
  methods: {
    goBack() {
      this.$router.back()
    },
    handleChangeLogin() {
      if (this.user.token) {
        this.$actions.setGlobalState({
          user: {
            name: '',
            head: '',
            token: ''
          },
        });
      } else {
        this.$router.push(`/login?redirect=${this.$route.path}`)
      }
    }
  }
}
</script>

<style lang="scss" >
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.el-container {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.el-header {
  background-color: #545c64;
  line-height: 60px;
  font-weight: 800;
  font-size: 24px;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0px 0px 10px 5px #999;
  .logo {
    width: 40px;
  }
  .title {
    letter-spacing: 4px;
  }
  .user {
    user-select: none;
    .el-dropdown-link {
      display: flex;
      align-items: center;
      justify-content: center;
      color: #fff;
      .name {
        margin: 0 10px;
      }
    }
  }
}
.el-aside {
  width: 200px !important;
  overflow-y: auto !important;
  overflow-x: hidden;
  height: calc(100vh - 60px);
  box-shadow: 0px 10px 10px 5px #999;
}
.el-menu {
  text-align: left;
  width: 100% !important;
  height: 100%;
}
.el-submenu .el-menu-item {
  min-width: initial !important;
  width: 100% !important;
}
</style>
